const app = getApp();
const getDefaultClickParams = ()=>(
  {
    touchStartTime: 0,
    touchEndTime: 0,
    lastTapTime: 0,
    lastTapTimeoutFunc: null
  }
)
let curScale = 1;
let clickParams = getDefaultClickParams();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: String,
      value: '',
      observer: function (newVal, oldVal, changedPath) {

      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgHeight: '100%',
    imgWidth: '100%',
    scaleValue: 1,
    scaleMax: 2,
    top: 0
  },
  attached: function () {
    curScale = 1;
    clickParams = getDefaultClickParams();
  },
 
  /**
   * 组件的方法列表
   */
  methods: {
    imgload: function (e) {
      let width = app.globalData.windowWidth;
      let height = app.globalData.windowHeight;

      let initImgW = e.detail.width;
      let initImgH = e.detail.height;

      let ratio2 = initImgW / initImgH;

      let imgWidth = width;
      let imgHeight = width / ratio2;

      let top = (height - imgHeight) / 2;

      if (imgHeight > height) {
        imgHeight = height;
        imgWidth = height * ratio2;
        top = 0;
      }

      let scaleMax = initImgW / imgWidth;

      this.setData({
        imgWidth: imgWidth + 'px',
        imgHeight: imgHeight + 'px',
        scaleMax: scaleMax,
        top: top + 'px'
      })
    },
    scaleChange: function (e) {
      curScale = e.detail.scale;
      this.triggerEvent('scaleChange',{
        curScale: curScale
      });
    },
    changeHandler: function (e) {
      console.log('change', e);
    },
    touchStart:function(e){
      clickParams.touchStartTime = e.timeStamp
    },
    touchEnd:function(e){
      clickParams.touchEndTime = e.timeStamp
    },
    handlerClick: function (e) {
      let me = this;
      let that = clickParams;
     
      if (that.touchEndTime - that.touchStartTime < 350) {
        let currentTime = e.timeStamp
        let lastTapTime = that.lastTapTime
        that.lastTapTime = currentTime

        if (currentTime - lastTapTime < 300) {
          console.log('双击', this.data.scaleMax)
          clearTimeout(that.lastTapTimeoutFunc);
          let scale = this.data.scaleMax;
          if (curScale > 1) {
            scale = 1
          }
          this.setData({
            scaleValue: scale
          })
        } else {
          that.lastTapTimeoutFunc = setTimeout(function () {
            curScale = 1;
            me.triggerEvent('noFullImg');
          }, 300);
        }
      }
      
    }
  }
})
